﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;

namespace FileThief
{
    /// <summary>
    /// 检测功能的类，负责处理检测磁盘的业务
    /// </summary>
    public class Detect
    {
        private static Detect dt = new Detect();
        public static Detect GetDetect()
        {
            return dt;
        }

        /// <summary>
        /// Detect构造函数，调试成功后改为private
        /// </summary>
        public Detect()
        {
          
        }

        public List<DriveInfo> GetAllDriveInfo()
        {
            List<DriveInfo> driveInfo = DriveInfo.GetDrives().ToList();
            List<DriveInfo> result = new List<DriveInfo>();

            foreach (DriveInfo i in driveInfo)
            {
                if (DriveFilter(i))
                {
                    result.Add(i);
                }
            }

            return result;
        }

        /// <summary>
        /// 输入新插入磁盘的盘符，在检查是否需要拷贝后，返回该磁盘的信息
        /// </summary>
        /// <param name="newName"></param>
        /// <returns></returns>
        public DriveInfo GetNewDriveInfo(string newName)
        {
            DriveInfo drive = new DriveInfo(newName);
            if (DriveFilter(drive))
            {
                return drive;
            }

            return null;
        }

        public bool DriveFilter(DriveInfo driveInfo)
        {
            if (driveInfo == null)
                return false;

            if (!driveInfo.IsReady)
                return false;

            string id = (new VolumeSerialNumberManager()).GetVolumeSerialNumber(driveInfo.Name);
            //Console.WriteLine(driveInfo.Name + " id is " + id);

            if (id == null)
                return false;

            #region NoCopyMarkDrives
            if (Settings.GetSettings().detect.IsNoCopyMarkDrivesOn)
            {
                if (Settings.GetSettings().detect.NoCopyMarkDrives.IsBlacklist == true)
                {
                    foreach (string i in Settings.GetSettings().detect.NoCopyMarkDrives.Blacklist)
                    {
                        #region 取得硬件物理序列号并对比，一样则返回false

                        if (i.Equals(id))
                            return false;

                        #endregion
                    }
                }

                if (Settings.GetSettings().detect.NoCopyMarkDrives.IsBlacklist == false)
                {
                    if (Settings.GetSettings().detect.NoCopyMarkDrives.Whitelist.Count == 0)
                        return false;
                    for (int i = 0; i < Settings.GetSettings().detect.NoCopyMarkDrives.Whitelist.Count; i++)
                    {
                        string j = Settings.GetSettings().detect.NoCopyMarkDrives.Whitelist[i];
                        {
                            if (j.Equals(id))
                                break;
                        }

                        if (i + 1 >= Settings.GetSettings().detect.NoCopyMarkDrives.Whitelist.Count)
                        {
                            return false;
                        }
                    }

                }
            }
            #endregion

            #region CopiedDriveType

            if (CopiedDriveTypeFilter(driveInfo.DriveType) == false)
                return false;

            #endregion

            return true;
        }

        public bool CopiedDriveTypeFilter(DriveType driveType)
        {
            if (Settings.GetSettings().detect.CopiedDriveType != 0)
            {
                int CopiedDriveType = 0;
                switch (driveType)
                {
                    case DriveType.Removable: CopiedDriveType = 1; break;
                    case DriveType.Fixed: CopiedDriveType = 2; break;
                    case DriveType.Network: CopiedDriveType = 3; break;
                    case DriveType.CDRom: CopiedDriveType = 4; break;
                    case DriveType.Ram: CopiedDriveType = 5; break;
                    default: return false;
                }

                if (CopiedDriveType != Settings.GetSettings().detect.CopiedDriveType)
                    return false;
                else
                    return true;
            }
            else
            {
                return true;
            }

        }

    }

    public class VolumeSerialNumberManager
    {
        private Dictionary<string, string> dic;

        public VolumeSerialNumberManager()
        {
            dic = new Dictionary<string, string>();
            SelectQuery sq = new SelectQuery("select * from win32_logicaldisk");
            System.Management.ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);

            try
            {
                foreach (System.Management.ManagementObject disk in mos.Get())
                {

                    //Console.WriteLine(disk["DeviceID"].ToString());
                    //Console.WriteLine(disk["VolumeSerialNumber"].ToString());
                    //Console.WriteLine(disk["DeviceID"].ToString().Split(':')[0]);

                    if (disk["DeviceID"] != null && disk["VolumeSerialNumber"] != null)
                        dic.Add(disk["DeviceID"].ToString().Split(':')[0], disk["VolumeSerialNumber"].ToString());
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("VolumeSerialNumberManager" + e.Message);
            }
        }

        /// <summary>
        /// 获得分区的序列号
        /// </summary>
        /// <param name="Name">盘符，例如C盘应输入"C:\"</param>
        /// <returns></returns>
        public string GetVolumeSerialNumber(string name)
        {
            try
            {
                //Console.WriteLine(name.Split(':')[0].ToUpper());
                return dic[name.Split(':')[0].ToUpper()];
            }
            catch (Exception)
            {
                return null;
            }
        }

    }
}
